﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logic.Interface;
using System.Windows.Forms;
using Magic;
using Logic.Mappings;

namespace Logic
{
    public class Player : IUpdatable
    {
        public int MaxHP { get; set; }
        public int HP { get; set; }
        public int BaseHP { get; set; }
        public int MaxMP { get; set; }
        public int MP { get; set; }
        public int BaseMp { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public int Z { get; set; }
        public int Gold { get; set; }
        public int Level { get; set; }
        public int ArmorClass { get; set; }
        public int Attack { get; set; }
        public int BaseAttack { get; set; }
        public int MaxAttack { get; set; }
        public int Dodge { get; set; }
        public int Archery { get; set; }
        public int StunBlow { get; set; }
        public int Peek { get; set; }
        public int Parry { get; set; }
        public int PickLock { get; set; }
        public int FirstAid { get; set; }
        public int Hide { get; set; }
        public int PowerfulBlow { get; set; }
        public int Meditate { get; set; }
        public int Rob { get; set; }
        public int Search { get; set; }
        public int Sneak { get; set; }
        public int ArmourPenetration { get; set; }
        public int RapidHealing { get; set; }
        public int HealingPotion { get; set; }
        public bool NeedHeal { get; set; }
        public bool NeedRecall { get; set; }
        public string HealMacro { get; set; }
        public double HealPercent { get; set; }
        public int CurrentPots { get; set; }
        public string RecallMacro { get; set; }
        public int TalkingNpcID { get; set; }
        public bool EnableRecall { get; set; }


        public Player(int pots)
        {
            CurrentPots = pots;
            NPC NpcList = new NPC();
        }

        /// <summary>
        /// Updates the player data from the memory.
        /// </summary>
        /// <param name="handle"></param>
        public void Update(IParameter parameters)
        {
            PlayerParameters player = (PlayerParameters)parameters;
            MaxHP = SMemory.ReadInt(player.Handle, 0x0836CAC);
            HP = SMemory.ReadInt(player.Handle, 0x0836CA4);
            BaseHP = SMemory.ReadInt(player.Handle, 0x0836C80);
            MaxMP = SMemory.ReadInt(player.Handle, 0x0836CB0);
            MP = SMemory.ReadInt(player.Handle, 0x0836CA8);
            BaseMp = SMemory.ReadInt(player.Handle, 0x0836CAC);
            X = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Y = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Z = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Gold = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Attack = SMemory.ReadInt(player.Handle, 0x0836CAC);
            BaseAttack = SMemory.ReadInt(player.Handle, 0x0836CAC);
            MaxAttack = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Dodge = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Archery = SMemory.ReadInt(player.Handle, 0x0836CAC);
            StunBlow = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Peek = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Parry = SMemory.ReadInt(player.Handle, 0x0836CAC);
            PickLock = SMemory.ReadInt(player.Handle, 0x0836CAC);
            FirstAid = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Hide = SMemory.ReadInt(player.Handle, 0x0836CAC);
            PowerfulBlow = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Meditate = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Rob = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Search = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Sneak = SMemory.ReadInt(player.Handle, 0x0836CAC);
            ArmourPenetration = SMemory.ReadInt(player.Handle, 0x0836CAC);
            RapidHealing = SMemory.ReadInt(player.Handle, 0x0836CAC);
            ArmorClass = SMemory.ReadInt(player.Handle, 0x0836CAC);
            Level = SMemory.ReadInt(player.Handle, 0x0836CAC);
           // HealingPotion = SMemory.ReadInt(player.Handle, 0x10671BE0);
            HealMacro = "{" + player.HealMacro + "}";
            HealPercent = player.HealPercent;
            NeedHeal = HP <= MaxHP * HealPercent;
            TalkingNpcID = SMemory.ReadInt(player.Handle, 0x083994C);
            //CurrentPots = player.CurrentPots;
            RecallMacro = "{" + player.RecallMacro + "}"; ;
            EnableRecall = player.EnableRecall;
            NeedRecall = CurrentPots == 0;
            Healing();
            Recalling();
        }

        private void Recalling()
        {
            if (EnableRecall && NeedRecall && NeedHeal)
                SendKeys.Send(RecallMacro);
        }

        public void Healing()
        {
            if (NeedHeal && CurrentPots > 0)
            {
                SendKeys.Send(HealMacro);
                CurrentPots -= 1;
            }
        }
    }
}
